#include "include.h"
#include "func.h"

bool power_off_check(void);
void lock_code_pwrsave(void);
void unlock_code_pwrsave(void);

AT(.com_text.sleep)
void lowpwr_tout_ticks(void)
{
    if(sys_cb.sleep_delay != -1L && sys_cb.sleep_delay > 0) {
        sys_cb.sleep_delay--;
    }
    if(sys_cb.pwroff_delay != -1L && sys_cb.pwroff_delay > 0) {
        sys_cb.pwroff_delay--;
    }
}

AT(.com_text.sleep)
bool sys_sleep_check(u32 *sleep_time)
{
    if(*sleep_time > sys_cb.sleep_wakeup_time) {
        *sleep_time = sys_cb.sleep_wakeup_time;
        return true;
    }
    return false;
}

//休眠定时器，500ms进一次
AT(.sleep_text.sleep)
uint32_t sleep_timer(void)
{
    uint32_t ret = 0;

    if (led_bt_sleep()) {
        sys_cb.sleep_wakeup_time = 50000/625;

#if VBAT_DETECT_EN
        bsp_saradc_init(adc_cb.channel);
        if ((xcfg_cb.lowpower_warning_en) && (sys_cb.vbat < ((u16)LPWR_WARNING_VBAT*100 + 2750))) {
            //低电需要唤醒sniff mode
            ret = 2;
        }
        saradc_exit(adc_cb.channel);
#endif // VBAT_DETECT_EN

    } else {
        sys_cb.sleep_wakeup_time = -1L;
    }
    if(sys_cb.pwroff_delay != -1L) {
        if(sys_cb.pwroff_delay > 5) {
            sys_cb.pwroff_delay -= 5;
        } else {
            sys_cb.pwroff_delay = 0;
            return 1;
        }
    }

    if ((PWRKEY_2_HW_PWRON) && (!IS_PWRKEY_PRESS())){
        ret = 1;
    }
    return ret;
}

AT(.text.lowpwr.sleep)
static void sfunc_sleep(void)
{
    uint32_t status, ret;
    uint32_t usbcon0, usbcon1;
    u8 pa_de, pb_de, pe_de, pf_de, pg_de;
    u8 p111_en;
    u16 adc_ch;

#if VBAT_DETECT_EN
    if (is_lowpower_vbat_warning()) {   //低电不进sniff mode
        return;
    }
#endif // VBAT_DETECT_EN

    printf("%s\n", __func__);

#if SYS_KARAOK_EN
    bsp_karaok_exit(AUDIO_PATH_KARAOK);
#endif
    bt_enter_sleep();

    gui_off();
    led_off();
    rled_off();

#if DAC_DNR_EN
    u8 sta = dac_dnr_get_sta();
    dac_dnr_set_sta(0);
#endif

    if (SLEEP_DAC_OFF_EN) {
        dac_power_off();                //dac power down
    } else {
        dac_channel_disable();          //only dac channel master disable
    }

    sys_set_tmr_enable(0, 0);

    saradc_exit(adc_cb.channel);        //close saradc及相关通路模拟
    adc_ch = adc_cb.channel;
    adc_cb.channel = BIT(ADCCH_VBAT) | BIT(ADCCH_BGOP);

#if MUSIC_UDISK_EN
    if (dev_is_online(DEV_UDISK)) {
        udisk_insert();                 //udisk需要重新enum
    }
#endif

    usbcon0 = USBCON0;                  //需要先关中断再保存
    usbcon1 = USBCON1;
    USBCON0 = BIT(5);
    USBCON1 = 0;

    if ((CLKCON0 & 0x70) != 0) {
        //close pll0
        PLL0CON &= ~BIT(18);                //pll10 sdm enable disable
        PLL0CON &= ~BIT(6);                 //pll0 analog diable
        PLL0CON &= ~BIT(12);                //pll0 ldo disable
    }

    //close pll1
    p111_en = pll1_disable();

    //io analog input
    pa_de = GPIOADE;
    pb_de = GPIOBDE;
    pe_de = GPIOEDE;
    pf_de = GPIOFDE;
    pg_de = GPIOGDE;
    GPIOADE = 0;
    GPIOBDE = 0;
    GPIOEDE = 0;
    GPIOFDE = 0;
    GPIOGDE = 0x3F; //MCP FLASH

    wakeup_disable();
    sleep_wakeup_config();

    sys_cb.sleep_wakeup_time = -1L;
    while(bt_is_sleep()) {
        WDT_CLR();
        status = bt_sleep_proc();

        if(status == 1) {
            ret = sleep_timer();
            if(ret) {
                if (ret == 1) {
                    sys_cb.pwrdwn_tone_en = 1;
                    func_cb.sta = FUNC_PWROFF;
                }
                break;
            }
        }

        if (wakeup_get_status()) {
            break;
        }
#if USER_TKEY
        if (tkey_wakeup_status()) {
            break;
        }
#endif // USER_TKEY
    }

    GPIOADE = pa_de;
    GPIOBDE = pb_de;
    GPIOEDE = pe_de;
    GPIOFDE = pf_de;
    GPIOGDE = pg_de;

    wakeup_disable();
    if (p111_en) {
        pll1_enable();
    }
    USBCON0 = usbcon0;
    USBCON1 = usbcon1;

    if ((CLKCON0 & 0x70) != 0) {
        //enable adpll
        PLL0CON |= BIT(12);                //PLL1 LDO enable
        delay_us(100);
        PLL0CON |= BIT(18);                //PLL1 SDM enable
        PLL0CON |= BIT(6);                 //PLL1 Analog enable
    }

    adc_cb.channel = adc_ch;
    bsp_saradc_init(adc_cb.channel);
    sys_set_tmr_enable(1, 1);

#if GUI_LCD_EN
    gui_init();
    f_bt.draw_update = 1;
#endif // GUI_LCD_EN

    if (DAC_FAST_SETUP_EN) {
        bsp_loudspeaker_mute();
        delay_5ms(2);
    }

    if (SLEEP_DAC_OFF_EN) {
        dac_restart();
    } else {
        dac_channel_enable();
    }

    bsp_change_volume(sys_cb.vol);
#if DAC_DNR_EN
    dac_dnr_set_sta(sta);
#endif
    bt_exit_sleep();
#if SYS_KARAOK_EN
    bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
#endif
    printf("sleep_exit\n");
}

AT(.text.lowpwr.sleep)
bool sleep_process(is_sleep_func is_sleep)
{
    if ((*is_sleep)()) {
        if ((!sys_cb.sleep_en) || (!port_2led_is_sleep_en())) {
            reset_sleep_delay();
            return false;
        }
        if(sys_cb.sleep_delay == 0) {
            sfunc_sleep();
            reset_sleep_delay();
            reset_pwroff_delay();
            return true;
        }
    } else {
        reset_sleep_delay();
        reset_pwroff_delay();
    }
    return false;
}

//红外关机进入sleep mode
ALIGNED(512)
AT(.text.lowpwr.sleep)
void func_sleepmode(void)
{
    uint32_t usbcon0, usbcon1;
    u8 pa_de, pb_de, pe_de, pf_de, pg_de;
    u16 adc_ch;
    u8 sys_clk, p111_en;
//    printf("%s\n", __func__);

    gui_display(DISP_OFF);
#if DAC_DNR_EN
    u8 sta = dac_dnr_get_sta();
    dac_dnr_set_sta(0);
#endif
#if SYS_KARAOK_EN
    bsp_karaok_exit(AUDIO_PATH_KARAOK);
#endif
    dac_power_off();                    //dac power down
    saradc_exit(adc_cb.channel);        //close saradc及相关通路模拟

    gui_off();
    led_off();
    rled_off();
    sys_clk = get_cur_sysclk();
    set_sys_clk(SYS_26M);
    sys_set_tmr_enable(0, 0);

    adc_ch = adc_cb.channel;
    adc_cb.channel = (ADCCH_VBAT|ADCCH_BGOP);

#if  MUSIC_UDISK_EN
    if (dev_is_online(DEV_UDISK)) {
        udisk_insert();                 //udisk需要重新enum
    }
#endif

    usbcon0 = USBCON0;                  //需要先关中断再保存
    usbcon1 = USBCON1;
    USBCON0 = BIT(5);
    USBCON1 = 0;

    //close pll0
    PLL0CON &= ~BIT(18);                //pll10 sdm enable disable
    PLL0CON &= ~BIT(6);                 //pll0 analog diable
    PLL0CON &= ~BIT(12);                //pll0 ldo disable

    //close pll1
    p111_en = pll1_disable();

    //io analog input
    pa_de = GPIOADE;
    pb_de = GPIOBDE;
    pe_de = GPIOEDE;
    pf_de = GPIOFDE;
    pg_de = GPIOGDE;
    GPIOADE = 0;
    GPIOBDE = 0;
    GPIOEDE = 0;
    GPIOFDE = 0;
    GPIOGDE = 0x3F;     //MCP FLASH

#if IRRX_HW_EN
    IR_CAPTURE_PORT();
#endif // IRRX_HW_EN
    wakeup_disable();
//    sleep_wakeup_config();                  //按键唤醒配置
    WDT_DIS();

    while(1) {
        LPMCON |= BIT(0);                   //Sleep mode enable
        asm("nop");asm("nop");asm("nop");

#if IRRX_HW_EN
        if (get_irkey() == KEY_IR_POWER) {
            ir_key_clr();
            break;
        }
#endif // IRRX_HW_EN

//        //io wakeup
//        if (wakeup_get_status()) {
//            break;
//        }
    }

    WDT_EN();

    GPIOADE = pa_de;
    GPIOBDE = pb_de;
    GPIOEDE = pe_de;
    GPIOFDE = pf_de;
    GPIOGDE = pg_de;

    wakeup_disable();

    //enable adpll
    PLL0CON |= BIT(12);                //PLL1 LDO enable
    delay_us(100);
    PLL0CON |= BIT(18);                //PLL1 SDM enable
    PLL0CON |= BIT(6);                 //PLL1 Analog enable
    USBCON0 = usbcon0;
    USBCON1 = usbcon1;

    set_sys_clk(sys_clk);              //还原系统时钟
    if (p111_en) {
        pll1_enable();
    }

    adc_cb.channel = adc_ch;
    bsp_saradc_init(adc_cb.channel);
    sys_set_tmr_enable(1, 1);

    if (DAC_FAST_SETUP_EN) {
        bsp_loudspeaker_mute();
        delay_5ms(2);
    }
    dac_restart();
    bsp_change_volume(sys_cb.vol);
    gui_init();
    gui_display(DISP_POWERON);
#if DAC_DNR_EN
    dac_dnr_set_sta(sta);
#endif
#if SYS_KARAOK_EN
    bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
#endif
    func_cb.sta = func_cb.last;
    func_cb.last = FUNC_NULL;
}

#define MDMSFR_BASE    0x00007c00
#define FMRXANGCON0    SFR_RW (SFR5_BASE + 0x18*4)
#define FMRXANGCON2    SFR_RW (SFR5_BASE + 0x1a*4)
#define RFPWRCON0      SFR_RW (MDMSFR_BASE + 0x48*4)  //RFPOWER_CNTL
#define RFTXRSV        SFR_RW (MDMSFR_BASE + 0x38*4)  //PLL_RC_CALIB_STAT
#define RFSXRSV        SFR_RW (MDMSFR_BASE + 0x39*4)  //FILTER_RC_CALIB_STAT
#define RFRXRSV        SFR_RW (MDMSFR_BASE + 0x3c*4)  //VCOFC_ICP_CALIBSTAT
#define RFPLLCON0      SFR_RW (MDMSFR_BASE + 0x1c*4)  //RPLLCNTL0
#define RFPLLCON1      SFR_RW (MDMSFR_BASE + 0x1d*4)  //RPLLCNTL1
#define RFPLLCON2      SFR_RW (MDMSFR_BASE + 0x21*4)  //RFPLLSDCNTL1
#define RFPLLCON3      SFR_RW (MDMSFR_BASE + 0x20*4)  //RFPLLSDCNTL0
//硬开关方案，低电时，进入省电状态
ALIGNED(512)
AT(.text.lowpwr.pwroff)
void sfunc_lowbat(void)
{
    WDT_DIS();

    USBCON0 = BIT(5);
    USBCON1 = 0;
    GPIOADE = 0;
    GPIOBDE = 0;
    GPIOEDE = 0;
    GPIOFDE = 0;
    GPIOGDE = 0x3F; //MCP FLASH

    FMRXANGCON0 &= ~(BIT(0) | BIT(2));
    FMRXANGCON2 = 0;
    RFPWRCON0 = 0;
    RFTXRSV   = 0;
    RFSXRSV   = 0;
    RFRXRSV   = 0;
    RFPLLCON0 = 0;
    RFPLLCON1 = 0;
    RFPLLCON2 = 0;
    RFPLLCON3 = 0;

    CLKCON0 |= BIT(0);                  //enable RC
    CLKCON0 = (CLKCON0 & ~(0x3 << 2));  //system clk select RC

    PWRCON0 = (PWRCON0 & ~0xf) | (0x2);            //VCORES3~0  //5  //467
    PWRCON0 = (PWRCON0 & ~(0xF<<4)) | (0x0<<4);    //VIOS3~0   //4   //
    PWRCON0 &= ~BIT(8);   //bat div2 en
    PWRCON0 &= ~BIT(22);   //bat div2 en

    PLL0CON &= ~BIT(13);                //PLL0 SDM Dis
    PLL0CON &= ~BIT(6);                 //PLL0 Analog Dis
    PLL0CON &= ~BIT(12);                //PLL0 LDO DIS

    XO26MCON &= ~(0xf<<24);             //X26 output DIS

    XO26MCON &= ~BIT(10);               //X26 DIS
    XO26MCON &= ~BIT(9);                //X26 LDO DIS

    LPMCON |= BIT(0);                   //Sleep mode enable
    asm("nop");asm("nop");asm("nop");
    while(1);
}

//软开关方案，POWER键/低电时，进入关机状态
ALIGNED(512)
AT(.text.lowpwr.pwroff)
void sfunc_pwrdown(u8 vusb_wakeup_en)
{
    LOUDSPEAKER_MUTE_DIS();
    WDT_DIS();

    PICCONCLR = BIT(0);                 //Globle IE disable
    CLKCON0 |= BIT(0);                  //enable RC
    CLKCON0 = (CLKCON0 & ~(0x3 << 2));  //system clk select RC

    PLL0CON &= ~BIT(13);                //PLL0 SDM Dis
    PLL0CON &= ~BIT(6);                 //PLL0 Analog Dis
    PLL0CON &= ~BIT(12);                //PLL0 LDO DIS

    XO26MCON &= ~(0xf<<24);             //X26 output DIS

    XO26MCON &= ~BIT(10);               //X26 DIS
    XO26MCON &= ~BIT(9);                //X26 LDO DIS
    uint rtccon0 = RTCCON0;
    uint rtccon3 = RTCCON3;
#if CHARGE_EN
    if (CHARGE_DC_WAKEUP_EN) {
        rtccon3 |= BIT(11);             //vusb insert wakeup enable
    }
#endif

    RTCCON3 = rtccon3 & ~BIT(10);  //关WK PIN，再打开，以清除Pending
    RTCCON0 = rtccon0 & ~0x04;    //只关LVD，保留RTCRAM与RTC

    rtccon3 &= ~0x0F;                   //关VDDCORE AON VDDCORE VDDIO VDDBUCK
    rtccon3 |= BIT(6);                  //Core power down enable
    rtccon3 |= BIT(10);                 //WK pin wake up enable

    RTCCON |= BIT(5);                   //PowerDown Reset，如果有Pending，则马上Reset
    RTCCON3 = rtccon3;
    LPMCON |= BIT(0);
    asm("nop");asm("nop");asm("nop");
    while (1);
}


#if USER_TKEY
extern tkey_cfg_t *pscan_tkcb;
#endif
//系统关闭所有无关模块,系统时钟只跑RC，检测IO高低状态唤醒,唤醒后不复位
ALIGNED(512)
AT(.text.lowpwr.sleep)
void func_poweroff_runrc(void)
{
    uint32_t usbcon0, usbcon1;
    u8 pa_de, pb_de, pe_de, pf_de, pg_de;
    u32 pwrcon0,pll0con,xo26mcon,clkcon0,fmrxangcon0;
    u32 rfpwrcon0, rftxrsv, rfsxrsv, rfrxrsv, rfpllcon0;
    u32 rtccon3, rtccon4, rtccon5;
    u16 adc_ch;
    u8 sys_clk, p111_en;
    printf("%s\n", __func__);
    gui_display(DISP_OFF);
#if DAC_DNR_EN
    u8 sta = dac_dnr_get_sta();
    dac_dnr_set_sta(0);
#endif
#if SYS_KARAOK_EN
    bsp_karaok_exit(AUDIO_PATH_KARAOK);
#endif
    dac_power_off();                    //dac power down
    saradc_exit(adc_cb.channel);        //close saradc及相关通路模拟

    gui_off();
    led_off();
    rled_off();
    sys_clk = get_cur_sysclk();
    set_sys_clk(SYS_2M);
    sys_set_tmr_enable(0, 0);
    adc_ch = adc_cb.channel;

#if  MUSIC_UDISK_EN
    if (dev_is_online(DEV_UDISK)) {
        udisk_insert();                 //udisk需要重新enum
    }
#endif
    usbcon0 = USBCON0;                  //需要先关中断再保存
    usbcon1 = USBCON1;
    USBCON0 = BIT(5);
    USBCON1 = 0;
    pwrcon0 = PWRCON0;
    pll0con = PLL0CON;
    rtccon3 = RTCCON3;
    rtccon4 = RTCCON4;
    rtccon5 = RTCCON5;
    rfpwrcon0 = RFPWRCON0;
    rftxrsv = RFTXRSV ;
    rfrxrsv = RFRXRSV ;
    rfsxrsv = RFSXRSV ;
    rfpllcon0 = RFPLLCON0;
    fmrxangcon0 = FMRXANGCON0;
    xo26mcon = XO26MCON;
    clkcon0 = CLKCON0;

    //io analog input
    pa_de = GPIOADE;
    pb_de = GPIOBDE;
    pe_de = GPIOEDE;
    pf_de = GPIOFDE;
    pg_de = GPIOGDE;
    GPIOADE = 0;
    GPIOBDE = 0;
    GPIOEDE = 0;
    GPIOFDE = 0;
    GPIOGDE = 0x3F;     //MCP FLASH

    WDT_DIS();
    CLKCON0 &= ~(BIT(2) | BIT(3));                  //sysclk sel rc2m

    p111_en = pll1_disable();

    PLL0CON &= ~BIT(13);                //PLL0 SDM Dis
    PLL0CON &= ~BIT(6);                 //PLL0 Analog Dis
    PLL0CON &= ~BIT(12);                //PLL0 LDO DIS

    RFPWRCON0 = 0;
    RFTXRSV =0;
    RFRXRSV = 0;
    RFSXRSV = 0;
    RFPLLCON0 = 0;
    FMRXANGCON0 = 0;

    RTCCON4 &= ~BIT(15);
    RTCCON5 |= (0x3f<<2);
    RTCCON4 = ((RTCCON4 & ~(0X1F)) | 0xa);
    RTCCON3 &= ~BIT(4);

    PWRCON0 = (PWRCON0 & ~0xf) | (0x2);            //VCORES3~0  //5  //467
    PWRCON0 = (PWRCON0 & ~(0xF<<4)) | (0x0<<4);    //VIOS3~0   //4   //
    PWRCON0 &= ~BIT(8);   //bat div2 en
    PWRCON0 &= ~BIT(22);   //bat div2 en

    XO26MCON &= ~(0xf<<24);             //X26 output DIS
    XO26MCON &= ~BIT(10);               //X26 DIS
    XO26MCON &= ~BIT(9);                //X26 LDO DIS

#if USER_TKEY
    CLKCON1 &= ~BIT(24);                        //tk_clksrc select rc2m_clk
    CLKGAT1 |= BIT(17);                         //tk_clksrc enable
    u16 key = NO_KEY;
    u16 wakeup = false;
    tkey_cail_set(40,10,1,1,2,10);
    pscan_tkcb->tkpthd = 3;
    pscan_tkcb->short_press_times = 2;
    while(1){
        void bsp_tkey_change_channel(void);
        bsp_tkey_change_channel();
        TKCON |= BIT(20);                      //TKEY kick start.
        delay_us(1000);
        for(u8 i = 0;i < 4;i++){                //扫描4路触摸通道
            key = tkey_get_key(i);
            if (key == KS_SHORT_PRESS) {       //短按状态
                //printf("touch key[%d] short press\n",i);
                if(i == 1){                     //如果是PB2(ch1)短按
                    wakeup = true;
                }
            }
        }
        if( wakeup == true){
            wakeup = false;
            break;
        }
    }
    tkey_cail_set(40,400,5,5,29,100);
    pscan_tkcb->tkpthd = 30;
    pscan_tkcb->short_press_times = 15;
    CLKCON1 |= BIT(24);                //tk_clksrc select x26m_clkdiv8
#else
    //PA6拉低唤醒测试
    GPIOADE |= BIT(6);
    GPIOADIR |= BIT(6);
    GPIOAPU |= BIT(6);
    u32 key_cnt = 0;
    while(1) {
        if(GPIOA & BIT(6)) {
            key_cnt = 0;
        } else {    //有IO按键按下
            key_cnt++;
        }
        if (key_cnt > 20) {
            break;
        }
    }
#endif
    XO26MCON = xo26mcon;
    delay_us(10);
    RTCCON3 = rtccon3;
    RTCCON4 = rtccon4;
    RTCCON5 = rtccon5;

    RFPWRCON0 = rfpwrcon0;
    RFTXRSV   = rftxrsv  ;
    RFRXRSV   = rfrxrsv  ;
    RFSXRSV   = rfsxrsv  ;
    RFPLLCON0 = rfpllcon0;
    FMRXANGCON0 = fmrxangcon0;

    PWRCON0 = pwrcon0;

    delay_us(10);
    PLL0CON = pll0con;
    delay_us(10);
    //enable pll
    PLL0CON |= BIT(12);                //PLL0 LDO enable
    delay_us(100);
    PLL0CON |= BIT(18);                //PLL0 SDM enable
    PLL0CON |= BIT(6);                 //PLL0 Analog enable
    delay_us(100);

    USBCON0 = usbcon0;
    USBCON1 = usbcon1;
    GPIOADE = pa_de;
    GPIOBDE = pb_de;
    GPIOEDE = pe_de;
    GPIOFDE = pf_de;
    GPIOGDE = pg_de;
    WDT_EN();
    CLKCON0 = clkcon0;
    set_sys_clk(sys_clk);              //还原系统时钟
    printf("wake up,continue run\n");
    if (p111_en) {
        pll1_enable();
    }
    adc_cb.channel = adc_ch;
    bsp_saradc_init(adc_cb.channel);
    sys_set_tmr_enable(1, 1);

    if (DAC_FAST_SETUP_EN) {
        bsp_loudspeaker_mute();
        delay_5ms(2);
    }
    dac_restart();
    bsp_change_volume(sys_cb.vol);
    gui_init();
    gui_display(DISP_POWERON);
#if DAC_DNR_EN
    dac_dnr_set_sta(sta);
#endif
#if SYS_KARAOK_EN
    bsp_karaok_init(AUDIO_PATH_KARAOK, func_cb.sta);
#endif
    func_cb.sta = func_cb.last;
    func_cb.last = FUNC_NULL;
}

AT(.text.lowpwr.pwroff)
void func_pwroff(int pwroff_tone_en)
{
    printf("%s\n", __func__);
    led_power_down();
#if WARNING_POWER_OFF
    if (SOFT_POWER_ON_OFF) {
        if (pwroff_tone_en == 1) {
            mp3_res_play(RES_BUF_POWEROFF_MP3, RES_LEN_POWEROFF_MP3);
        } else if (pwroff_tone_en == 2) {
#if WARNING_FUNC_AUX
            mp3_res_play(RES_BUF_AUX_MODE_MP3, RES_LEN_AUX_MODE_MP3);
#endif // WARNING_POWER_OFF
        }
    }
#endif // WARNING_POWER_OFF

    gui_off();

    if (SOFT_POWER_ON_OFF) {
        if (!PWRKEY_2_HW_PWRON) {
            while (IS_PWRKEY_PRESS()) { //等待PWRKWY松开
                delay_5ms(1);
                WDT_CLR();
            }
        }
#if SYS_KARAOK_EN
        bsp_karaok_exit(AUDIO_PATH_KARAOK);
#endif
        dac_power_off();                    //dac power down
        if (CHARGE_DC_IN()) {
            if (power_off_check()) {
                return;
            }
        }
        saradc_exit(adc_cb.channel);        //close saradc及相关通路模拟
        if ((PWRKEY_2_HW_PWRON) && (sys_cb.poweron_flag)) {
            RTCCON1 |= BIT(6);            //WK PIN High level wakeup      //
        }
        sfunc_pwrdown(1);
    } else {
#if SYS_KARAOK_EN
        bsp_karaok_exit(AUDIO_PATH_KARAOK);
#endif
        dac_power_off();                    //dac power down
        saradc_exit(adc_cb.channel);        //close saradc及相关通路模拟

        sfunc_lowbat();                     //低电关机进入Sleep Mode
    }
}
